﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace LightManager.UserControls
{
    internal partial class EffectListControl : UserControl
    {
        private GameManager manager;

        public EffectListControl()
        {
            InitializeComponent();
        }

        public GameManager Manager
        {
            set
            {
                this.manager = value;
                value.Actions.ListChanged += this.Actions_ListChanged;
            }
        }

        void Actions_ListChanged(object sender, ListChangedEventArgs e)
        {
            this.flpEffects.SuspendLayout();
            this.flpEffects.Controls.Clear();
            for (int i = this.manager.Actions.Count - 1; i >= 0; i--)
            {
                Action a = this.manager.Actions[i];
                EffectControl control = new EffectControl();
                control.DataSource = a;
                control.Click += (mySender, myArgs) => this.control_Click(mySender, myArgs, a);
                control.MouseEnter += (mySender, myArgs) => this.control_MouseEnter(mySender, myArgs, a);
                control.MouseLeave += (mySender, myArgs) => this.control_MouseLeave(mySender, myArgs, a);
                this.flpEffects.Controls.Add(control);
            }
            this.flpEffects.ResumeLayout();
        }

        void control_MouseLeave(object sender, EventArgs e, Action a)
        {
            this.lblDescription.Visible = false;
        }

        void control_MouseEnter(object sender, EventArgs e, Action a)
        {
            this.lblDescription.Visible = true;
            this.lblDescription.Text = string.Format("{0} {1} {2} per {3} turni", a.From, a.Effect.GetType().Name.Replace("Effect", ""), a.Target.Name, a.Duration - a.InProgress).Trim();
        }

        void control_Click(object sender, EventArgs e, Action a)
        {
            DialogResult result = MessageBox.Show(string.Format("{0}\nRimuovere questo effetto?", this.lblDescription.Text), "Pending Action remove", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
            if (result != DialogResult.OK) return;
            this.manager.RemoveAction(a);
        }
    }
}
